﻿using System;
using System.Net.Mail;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace Chatu.MsbuildTasks
{
    public class EmailTask : Task
    {
        #region Fields

        private string _from;
        private string _fromName;
        private string[] _to;
        private string[] _cc;
        private string[] _bcc;
        private string _subject;
        private string _body;
        private bool _isBodyHtml = false;

        private string _smtpServer = "localhost";
        private string _username;
        private string _password;
        private int _port = 25;
        private bool _enableSSL = false;

        #endregion

        #region Properties

        [Required]
        public string From
        {
            get { return _from; }
            set { _from = value; }
        }

        public string FromName
        {
            get { return _fromName; }
            set { _fromName = value; }
        }

        [Required]
        public string[] To
        {
            get { return _to; }
            set { _to = value; }
        }

        public string[] CC
        {
            get { return _cc; }
            set { _cc = value; }
        }

        public string[] Bcc
        {
            get { return _bcc; }
            set { _bcc = value; }
        }

        [Required]
        public string Subject
        {
            get { return _subject; }
            set { _subject = value; }
        }

        [Required]
        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }

        public bool IsBodyHtml
        {
            get { return _isBodyHtml; }
            set { _isBodyHtml = value; }
        }

        public string SmtpServer
        {
            get { return _smtpServer; }
            set { _smtpServer = value; }
        }

        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public int Port
        {
            get { return _port; }
            set { _port = value; }
        }

        public bool EnableSSL
        {
            get { return _enableSSL; }
            set { _enableSSL = value; }
        }

        #endregion

        #region Methods

        public override bool Execute()
        {
            bool toReturn = true;

            SmtpClient sc = new SmtpClient();
            MailMessage mm = new MailMessage();

            sc.Host = _smtpServer;
            sc.EnableSsl = _enableSSL;
            sc.Port = _port;

            if (string.IsNullOrEmpty(_username))
                sc.UseDefaultCredentials = true;
            else
                sc.Credentials = new System.Net.NetworkCredential(_username, _password);

            mm.From = new MailAddress(_from, _fromName ?? _from);
            mm.Subject = _subject;
            mm.Body = _body;
            mm.IsBodyHtml = _isBodyHtml;

            foreach (string to in _to)
            {
                mm.To.Add(new MailAddress(to));
            }

            if (_cc != null && _cc.Length > 0)
            {
                foreach (string cc in _cc)
                {
                    mm.CC.Add(new MailAddress(cc));
                }
            }

            if (_bcc != null && _bcc.Length > 0)
            {
                foreach (string bcc in _bcc)
                {
                    mm.Bcc.Add(new MailAddress(bcc));
                }
            }

            try
            {
                sc.Send(mm);
                Log.LogMessage("Email was sent");
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex);
                toReturn = false;
            }
            finally
            {
                mm.Dispose();
            }

            return toReturn;
        }

        #endregion
    }
}